package com.pig4cloud.pigx.auth.config;

import com.pig4cloud.pigx.common.security.grant.CustomAppAuthenticationProvider;
import com.pig4cloud.pigx.common.security.mobile.MobileLoginSuccessHandler;
import com.pig4cloud.pigx.common.security.mobile.MobileSecurityConfigurer;
import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

/**
 * @description:
 * @Author 兔子不吃窝边曹
 * @create 2022/7/15 17:25
 **/
@Configuration
@RequiredArgsConstructor
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {

    private final PigxUserDetailsService pigxDefaultUserDetailsServiceImpl;

    /**
     * 手机号码登录的相关配置
     *
     * @return
     */
    @Bean
    public MobileSecurityConfigurer mobileSecurityConfigurer() {
        return new MobileSecurityConfigurer();
    }

    @Bean
    public AuthenticationSuccessHandler mobileLoginSuccessHandler() {
        return new MobileLoginSuccessHandler();
    }

    @Override
    @SneakyThrows
    protected void configure(HttpSecurity http) {
        http
                .authorizeRequests().antMatchers("/token/**", "/actuator/**", "/mobile/**").permitAll()
                .anyRequest().authenticated()
                .and().csrf().disable();

        // 将 mobileSecurityConfigurer 放入spring security 配置中
        http.apply(mobileSecurityConfigurer());
    }

    /**
     * 自定义 provider 列表注入
     *
     * @param auth AuthenticationManagerBuilder
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) {
        // 自定义的认证模式
        CustomAppAuthenticationProvider provider = new CustomAppAuthenticationProvider();
        provider.setUserDetailsService(pigxDefaultUserDetailsServiceImpl);
        auth.authenticationProvider(provider);
    }

    /**
     * 不拦截静态资源
     *
     * @param web
     */
    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/favicon.ico", "/css/**", "/error");
    }

    @Bean
    @Override
    @SneakyThrows
    public AuthenticationManager authenticationManagerBean() {
        return super.authenticationManagerBean();
    }

    /**
     * 密码处理器
     * return new BCryptPasswordEncoder()--->return PasswordEncoderFactories.createDelegatingPasswordEncoder()
     * https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-storage-format
     *
     * @return 动态密码处理器 {类型}密文
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

}

